/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.dao.baseData;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.chenweihuacwh.dto.DTO;
import com.javaeye.chenweihuacwh.dto.baseData.Logistics;
import com.javaeye.chenweihuacwh.web.PageInfo;

public class LogisticsDAOHibernate extends HibernateDaoSupport implements LogisticsDAO {
	
	@SuppressWarnings("unchecked")
	public List getPageData(final DTO contidions, final PageInfo pageInfo) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				int pageIndex = pageInfo.getPageIndex();
				int pageSize = pageInfo.getPageSize();
				Logistics logistics = (Logistics)contidions;
				Criteria crit = session.createCriteria(Logistics.class);
				if (!"".equals(logistics.getName().trim())) {
					crit.add(Restrictions.like("name", "%" + logistics.getName().trim() + "%"));
				}
				if (!"-1".equals(logistics.getCategory().trim())) {
					crit.add(Restrictions.eq("category", logistics.getCategory().trim()));
				}
				crit.setProjection(Projections.rowCount());
				Integer rowNumber = (Integer)crit.uniqueResult();
				pageInfo.setCount(rowNumber);
				
				crit.setProjection(null);//取得了总数后,为了能够再次取得对象列表
				crit.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);//当hibernate采用关联的时候不使用这个返回的是个对象数组，加上这个返回的是当前列表的对象。
				crit.setFirstResult(pageIndex * pageSize);
				crit.setMaxResults(pageSize);
				return crit.list();
			}
		};
		return getHibernateTemplate().executeFind(action);
	}
	
	@Override
	public void saveLogistics(Logistics logistics) {
		getHibernateTemplate().saveOrUpdate(logistics);
		logger.debug("Logistics id set to: " + logistics.getId());
	}

	@Override
	public Logistics getLogistics(int id) {
		return (Logistics)getHibernateTemplate().get(Logistics.class, id);
	}

	@Override
	public void removeLogistics(int id) {
		Object o = getHibernateTemplate().load(Logistics.class, id);
		getHibernateTemplate().delete(o);
	}
	
	@SuppressWarnings("unchecked")
	public List<Logistics> getAutoCompleteLogistics(final String name, final int size) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				final String hql = "select l.id, l.name from Logistics l where l.name like '%" + name.trim() + "%'";
				Query query = session.createQuery(hql);
				query.setFirstResult(0);
				query.setMaxResults(size);
				return query.list();
			}
		};
		return getHibernateTemplate().executeFind(action);
	}

}
